//
//  WSAppTools.m
//  GroupFourFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSAppTools.h"
#import <AFNetworking.h>


@implementation WSAppTools

+(void)getDataWithURL:(NSString *)urlStr block:(AppToolsBlock)block error:(AppToolsErrorBlock)errorBlock
{
    
    NSError *error2 = nil;
    NSString *urlEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:urlEncode parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //block回调
        block(responseObject);
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败==== %@",error);
        errorBlock(error2);
        
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [urlStr hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        block(result);

        
    }];
    
}

+(void)postDataWithURL:(NSString *)urlStr Para:(NSMutableDictionary *)para block:(AppToolsBlock)block Error:(AppToolsErrorBlock)errorBlock
{
    
    NSError *error2 = nil;
    
    AFHTTPSessionManager *netManager = [AFHTTPSessionManager manager];
    netManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [netManager POST:urlStr parameters:para constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSLog(@"post请求成功====%@", dic);
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"错误信息%@", error);
        errorBlock(error2);
    }];
    
}

+(void)getDataWithFileName:(NSString *)fileName block:(AppToolsBlock)block error:(AppToolsErrorBlock)errorBlock
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    
    NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath];
    
    block(array);
    
}

+ (CGFloat)heightForContent:(NSString *)string width:(CGFloat)width size:(NSInteger)sizes {
    
    CGSize size = CGSizeMake(width, 20000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:sizes] forKey:NSFontAttributeName];
    
    CGRect frame = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}


@end
